<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      class Set {
        constructor() {
          this.obj = {};
        }
        add(value) {
          if (this.has(value)) {
            return false;
          }
          this.obj[value] = value;
          return true;
        }
        remove(value) {
          if (!this.has(value)) {
            return false;
          }
          delete this.obj[value];
          return true;
        }
        has(value) {
          return this.obj.hasOwnProperty(value);
        }
        clear() {
          this.obj = {};
        }
        size() {
          return Object.keys(this.obj).length;
        }
        values() {
          return Object.keys(this.obj);
        }
        // 并集
        union(otherSet) {
          let unionSet = new Set(),
            values = this.values();
          for (let i = 0; i < values.length; i++) {
            unionSet.add(values[i]);
          }
          values = otherSet.values();
          for (let j = 0; j < values.length; j++) {
            unionSet.add(values[j]);
          }
          return unionSet;
        }
        // 交集
        intersection(otherSet) {
          let intersectionSet = new Set(),
            values = this.values();
          for (let i = 0; i < values.length; i++) {
            if (otherSet.has(values[i])) {
              intersectionSet.add(values[i]);
            }
          }
          return intersectionSet;
        }
        // 差集
        difference(otherSet) {
          let differenceSet = new Set(),
            values = this.values();
          for (let i = 0; i < values.length; i++) {
            if (!otherSet.has(values[i])) {
              differenceSet.add(values[i]);
            }
          }
          return differenceSet;
        }
        // 子集
        subSet(otherSet) {
          let values = this.values();
          for (let i = 0; i < values.length; i++) {
            if (!otherSet.has(values[i])) {
              return false;
            }
          }
          return true;
        }
      }

      let set = new Set();
      set.add("a");
      set.add("b");
      set.add("c");
      set.add("d");
      set.add("e");
      set.add("f");
      console.log(set);
      let set1 = new Set();
      set1.add("a1");
      set1.add("b2");
      set1.add("c3");
      set1.add("d4");
      set1.add("e5");
      set1.add("f");
      console.log(set.union(set1));
      console.log(set.intersection(set1));
      console.log(set.difference(set1));
      let set2 = new Set();
      set2.add("d");
      set2.add("e");
      set2.add("f");
      console.log(set2.subSet(set));
    </script>
  </body>
</html>
